﻿using GalaSoft.MvvmLight;
using LiveCharts;
using LiveCharts.Definitions.Series;
using LiveCharts.Helpers;
using LiveCharts.Wpf;
using SoftHelper.Model;
using SoftHelper.Tool;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using HandyControl;
using System.IO;
using System.Text;
using System.Windows.Controls;
using HandyControl.Controls;

namespace SoftHelper.ViewModel
{
    public delegate void RefreshHandler();
    public class AnalysisProjectViewModel :ViewModelBase
    {
        public String path;
        private int fileNumber;
        private AnalysisProjectDataModel analysisProjectData;
        private ObservableCollection<FileInfoModel> files;
        /// <summary>
        /// 刷新事件
        /// </summary>
        public static event RefreshHandler RefreshEvent;
        public AnalysisProjectDataModel AnalysisProjectData
        {
            get { return analysisProjectData; }
            set { analysisProjectData = value;RaisePropertyChanged("AnalysisProjectData"); }
        }
        public ObservableCollection<FileInfoModel> FileList
        {
            get { return files; }
            set { files = value;RaisePropertyChanged("FileList"); }
        }
        public int FileNumber
        {
            get { return fileNumber; }
            set { fileNumber = value; RaisePropertyChanged("FileNumber"); }
        }

        private MovieListBoxLanguageModel movieListBoxLanguage = new MovieListBoxLanguageModel();

        public MovieListBoxLanguageModel MovieListBoxLanguage
        {
            get { return movieListBoxLanguage; }
            set { movieListBoxLanguage = value;RaisePropertyChanged("MovieListBoxLanguage"); }
        }


        /// <summary>
        /// 将文件类型向右移动
        /// </summary>
        public RalyCommand ToRight
        {
            get
            {
                return new RalyCommand((o) =>
                {
                    ListBox listBox = o as ListBox;
                    object selectedItem = listBox.SelectedItem;
                    try
                    {
                        MovieListBoxLanguage.FilesTypesNeedList.Remove(selectedItem.ToString());
                        MovieListBoxLanguage.FilesTypesNoNeedList.Add(selectedItem.ToString());
                        List<String> temp = new List<String>(MovieListBoxLanguage.FilesTypesNeedList);
                        ChangeCondition(temp);
                    }
                    catch (Exception ex)
                    {
                       
                    }
                }, x => true);
            }
        }

        /// <summary>
        /// 将文件类型向左移动
        /// </summary>
        public RalyCommand ToLeft
        {
            get => new RalyCommand((o) =>
            {
                ListBox listBox = o as ListBox;
                object selectedItem = listBox.SelectedItem;
                try
                {
                    MovieListBoxLanguage.FilesTypesNoNeedList.Remove(selectedItem.ToString());
                    MovieListBoxLanguage.FilesTypesNeedList.Add(selectedItem.ToString());
                    List<String> temp = new List<String>(MovieListBoxLanguage.FilesTypesNeedList);
                    ChangeCondition(temp);
                }
                catch (Exception ex)
                {
                    
                }
            }, x => true);
        }


        public  AnalysisProjectViewModel(String path)
        {
            this.path = path;
            FileList = new ObservableCollection<FileInfoModel>();
            AnalysisProjectData = new AnalysisProjectDataModel();
            AnalysisProjectData.ProjectLanguage = new ProjectLanguageModel();
            init();
            initMoveListBox();
        }

        /// <summary>
        /// 初始化右侧移动框
        /// </summary>
        private void initMoveListBox()
        {
            //加载右侧的移动框
            MovieListBoxLanguage.FilesTypesNoNeedList = new ObservableCollection<string>();
            MovieListBoxLanguage.FilesTypesNeedList = new ObservableCollection<string>();
            foreach (var item in AnalysisProjectData.ProjectLanguage.FileTypeDictionary.Keys)
            {
                MovieListBoxLanguage.FilesTypesNeedList.Add(item);
            }
        }

        /// <summary>
        /// 当筛选条件发生改变的时候重新刷新界面
        /// </summary>
        /// <param name="LanguageTypes"></param>
        private async void ChangeCondition(List<String> LanguageTypes)
        {
            IOTool tool = new IOTool();
            tool.SetFilesType(LanguageTypes);
            tool.getAllFiles(path);
            IList<FileInfo> FileListsTemp = tool.FilesList;
            AnalysisProjectData.ProjectLanguage = IOTool.AnalysisProjectLanguage(FileListsTemp);
            foreach (FileInfo file in FileListsTemp)
            {
                FileList.Add(new FileInfoModel()
                {
                    filePath = file.FullName.Substring(file.FullName.LastIndexOf('\\')),
                    fileType = file.FullName.Substring(file.FullName.LastIndexOf('.') + 1)
                });
            }
            AnalysisProjectData.CodeLines = await IOTool.getProjectCodeLines(FileListsTemp);
            //加载右侧数据
            AnalysisProjectData.PropjectFiles = FileListsTemp.Count;
            AnalysisProjectData.ProjectChars = await IOTool.getProjectChars(FileListsTemp);

            //获得信息

            List<string> titles = new List<string>();
            List<double> pieValues = new List<double>();
            //赋值 加载图表信息
            titles.AddRange(AnalysisProjectData.ProjectLanguage.FileTypeDictionary.Keys);
            pieValues.AddRange(AnalysisProjectData.ProjectLanguage.FileTypeDictionary.Values);
            ChartValues<double> chartvalue = new ChartValues<double>();
            AnalysisProjectData.FilePieseries = new SeriesCollection();
            for (int i = 0; i < titles.Count; i++)
            {
                chartvalue = new ChartValues<double>();
                chartvalue.Add(pieValues[i]);
                PieSeries series = new PieSeries();
                series.DataLabels = true;
                series.Title = titles[i];
                series.Values = chartvalue;
                AnalysisProjectData.FilePieseries.Add(series);
            }
        }


        /// <summary>
        /// 初始化界面
        /// </summary>
        private async void init()
        {
            //加载左侧文件列表
            IOTool tool = new IOTool();
            tool.getAllFiles(path);

            IList<FileInfo> FileLists = tool.FilesList;
            AnalysisProjectData.ProjectLanguage = IOTool.AnalysisProjectLanguage(FileLists);
            foreach (FileInfo file in FileLists)
            {
                FileList.Add(new FileInfoModel() { 
                    filePath = file.FullName.Substring(file.FullName.LastIndexOf('\\')) ,
                    fileType = file.FullName.Substring(file.FullName.LastIndexOf('.')+1)
                });
            }
            AnalysisProjectData.CodeLines = await IOTool.getProjectCodeLines(FileLists);
            //加载右侧数据,文件数字符数
            AnalysisProjectData.PropjectFiles = FileList.Count;
            AnalysisProjectData.ProjectChars = await IOTool.getProjectChars(FileLists);

            //获得信息

            List<string> titles = new List<string>();
            List<double> pieValues = new List<double>();
            //赋值 加载图表信息
            titles.AddRange(AnalysisProjectData.ProjectLanguage.FileTypeDictionary.Keys);
            pieValues.AddRange(AnalysisProjectData.ProjectLanguage.FileTypeDictionary.Values);
            ChartValues<double> chartvalue = new ChartValues<double>();
            AnalysisProjectData.FilePieseries = new SeriesCollection();
            for (int i = 0; i < titles.Count; i++)
            {
                chartvalue = new ChartValues<double>();
                chartvalue.Add(pieValues[i]);
                PieSeries series = new PieSeries();
                series.DataLabels = true;
                series.Title = titles[i];
                series.Values = chartvalue;
                AnalysisProjectData.FilePieseries.Add(series);
            }


            //注册刷新数据的事件
            RefreshEvent += AnalysisProjectViewModel_Refresh;
            RefreshEvent += AnalysisProjectViewModel_RefreshEvent_RefMovieLettterBox;
        }

        /// <summary>
        /// 刷新右侧的移动框
        /// </summary>
        private void AnalysisProjectViewModel_RefreshEvent_RefMovieLettterBox()
        {
            initMoveListBox();
        }

        /// <summary>
        /// 刷新数据的方法
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void AnalysisProjectViewModel_Refresh()
        {
            init();
        }

        /// <summary>
        /// 刷新
        /// </summary>
        public static void RefreshMethod()
        {
            RefreshEvent();
        }
    }
}
